package user

import (
	"github.com/gin-gonic/gin"
	"go-interview-forum/resp"
	"go-interview-forum/service/user"
	"strconv"
)

type UpdateUserDetail struct {
	Nickname string `json:"nickname"`
	Avatar   string `json:"avatar"`
}

func UpdateUserInfo(c *gin.Context) {
	var userDetail UpdateUserDetail
	err := c.ShouldBind(&userDetail)
	if err != nil {
		resp.QueryErrorResp(c, "参数错误")
		return
	}

	userId, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
	}

	id, err := strconv.Atoi(userId.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	err = user.UpdateUserInfo(id, userDetail.Nickname, userDetail.Avatar)
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, err.Error())
		return
	}

	resp.SuccessResp(c, nil)
}
